package com.csust.automaticteach.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csust.automaticteach.constant.OrderConstant;
import com.csust.automaticteach.model.dto.amessage.AmessageQueryRequest;
import com.csust.automaticteach.model.entity.Amessage;
import com.csust.automaticteach.model.vo.amessage.AmessageVO;
import com.csust.automaticteach.service.AmessageService;
import com.csust.automaticteach.mapper.AmessageMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 钰玟
* @description 针对表【t_amessage(消息表)】的数据库操作Service实现
* @createDate 2025-08-20 14:54:00
*/
@Service
public class AmessageServiceImpl extends ServiceImpl<AmessageMapper, Amessage>
    implements AmessageService{
    /**
     * 获取查询条件
     * @param amessageQueryRequest 查询条件
     * @return 查询条件
     */
    @Override
    public QueryWrapper<Amessage> getAmessageQueryWrapper(AmessageQueryRequest amessageQueryRequest) {
        //1.提取字段
        String type = amessageQueryRequest.getType();
        String allowRole = amessageQueryRequest.getAllowRole();
        String sortField = amessageQueryRequest.getSortField();
        String sortOrder = amessageQueryRequest.getSortOrder();
        //2.创建查询条件
        QueryWrapper<Amessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(type), "type", type);
        queryWrapper.eq(StrUtil.isNotBlank(allowRole), "allowRole", allowRole);
        queryWrapper.orderBy(StrUtil.isNotBlank(sortField), OrderConstant.ASC.equals(sortOrder), sortField);
        //3.返回查询条件
        return queryWrapper;
    }
    /**
     * 获取VO
     * @param record 实体
     * @return VO列表
     */
    @Override
    public AmessageVO getAmessageVOList(Amessage record) {
        AmessageVO amessageVO = new AmessageVO();
        BeanUtil.copyProperties(record,amessageVO);
        return amessageVO;
    }
    /**
     * 获取VO列表
     * @param records 实体列表
     * @return VO列表
     */
    @Override
    public List<AmessageVO> getAmessageVOList(List<Amessage> records) {
        return records.stream().map(record -> {
            AmessageVO amessageVO = new AmessageVO();
            BeanUtil.copyProperties(record,amessageVO);
            return amessageVO;
        }).toList();
    }
}




